﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;

namespace CodeTalk
{
    public class InputHandler
    {
        private SpeechRecognitionEngine recognizer;

        private SpeechMode CurrentMode { get; set; }

        private bool listening = false;
        public bool Listening
        {
            get { return listening; }
            set
            {
                if (value)
                {
                    if (!listening)
                    {
                        recognizer.RecognizeAsync(RecognizeMode.Multiple);
                        listening = true;
                    }
                }
                else
                {
                    if (listening)
                    {
                        recognizer.RecognizeAsyncStop();
                        listening = false;
                        //audioProgressBar.Value = 0;
                    }
                }
            }
        }

        public bool Init(SpeechMode CurrentMode)
        {
            this.CurrentMode = CurrentMode;

            recognizer = new SpeechRecognitionEngine();
            CurrentMode.Init(recognizer);

            recognizer.SetInputToDefaultAudioDevice();
            CurrentMode.Activate();

            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(OnSpeechRecognized);
            recognizer.AudioLevelUpdated += new EventHandler<AudioLevelUpdatedEventArgs>(OnAudioLevelUpdated);
            Listening = true;
            return true;
        }

        //public bool SetSpeechMode(SpeechMode mode)
        //{
        //    CurrentMode = mode;
        //    CurrentMode.Init(recognizer);
        //    CurrentMode.Activate();
        //    Listening = true;
        //    return true;
        //}

        public void Terminate()
        {
            Listening = false;
            if (CurrentMode != null) CurrentMode.Deactivate();
            if (recognizer != null)
            {
                recognizer.UnloadAllGrammars();
                recognizer.Dispose();
            }
        }

        void OnSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            CurrentMode.OnSpeechRecognized(sender, e);
        }

        void OnAudioLevelUpdated(object sender, AudioLevelUpdatedEventArgs e)
        {
            try
            {
                if (Listening)
                    CurrentMode.CurrentGuiHandler.SetAudioLevel(e.AudioLevel);
                else
                    CurrentMode.CurrentGuiHandler.SetAudioLevel(0);
            }
            catch (Exception)
            {
                return;
            }
        }

        public static string ReadSelectedText()
        {
            try
            {
                var oldContent = Clipboard.GetDataObject();
                SendKeys.SendWait("^c");
                System.Threading.Thread.Sleep(50);
                SendKeys.SendWait("^c");
                System.Threading.Thread.Sleep(50);
                string text = null;
                if (Clipboard.ContainsText())
                    text = Clipboard.GetText();

                Clipboard.SetDataObject(oldContent);
                return text;
            }
            catch (Exception e)
            {
                string s = e.ToString();
                return null;
            }
        }
    }
}
